/*

Server Side Validation Library: A C# library for validation.
Copyright (C) 2006  Chad Finsterwald, CoreWeb

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

*/

using System;
using System.Collections.Generic;
using System.Text;

namespace CoreWebLibrary.Validation
{
    [Serializable()]
    public class BrokenRules: ICloneable
    {
        #region Properties

        private Dictionary<string, BrokenRule> m_rules = new Dictionary<string,BrokenRule>() ;
        public Dictionary<string, BrokenRule>.ValueCollection Rules
        {
            get { return m_rules.Values ; }
        }

        public BrokenRule this[string name]
        {
            get { return m_rules[name]; }
        }

        public virtual int Count
        {
            get { return m_rules.Count; }
        }

        public virtual bool IsValid
        {
            get { return Count == 0 ? true : false; }
        }

        #endregion

        public BrokenRules()
        {
        }

        #region Methods

        public virtual void Assert(bool assertion, string name, string description)
        {
            Assert(assertion, new BrokenRule(name, description));
        }

        public virtual void Assert(bool assertion, BrokenRule br)
        {
            if (assertion == false)
                Add(br);
        }

        public virtual void Add(string name, string description)
        {
            Add(new BrokenRule(name, description));
        }

        public virtual void Add(BrokenRule br)
        {
            if ( IsBroken( br.Name ) == false )
                m_rules.Add(br.Name, br);
        }

        public virtual bool Remove(BrokenRule br)
        {
            return Remove(br.Name);
        }

        public virtual bool Remove(string name)
        {
            return m_rules.Remove(name);
        }

        public virtual void Clear()
        {
            m_rules.Clear();
        }

        public virtual bool IsBroken(BrokenRule br)
        {
            return IsBroken(br.Name);
        }

        public virtual bool IsBroken(string name)
        {
            return m_rules.ContainsKey(name);
        }

        public virtual void Merge(BrokenRules rules)
        {
            // only adds the rule if it is not already
            // contained in the collection.  This is a deep copy
            foreach (BrokenRule br in rules.Rules)
            {
                if ( IsBroken( br ) == false )
                    m_rules.Add(br.Name, new BrokenRule( br.Name, br.Description ) ) ;
            }
        }

        public object Clone()
        {
            BrokenRules rules = new BrokenRules();
            rules.Merge(this);
            return rules;
        }

        #endregion
    }
}
